<?php

declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Controller\Commerce_address;

use Plugin\Attractions\Commerce\Service\Commerce_address\CommerceAddressService;
use Plugin\Attractions\Commerce\Request\Commerce_address\CommerceAddressRequest;
use Plugin\Attractions\Commerce\Schema\Commerce_address\CommerceAddressSchema;
use App\Http\Admin\Middleware\PermissionMiddleware;
use App\Http\Common\Middleware\AccessTokenMiddleware;
use App\Http\Common\Middleware\OperationMiddleware;
use App\Http\Common\Result;
use App\Http\CurrentUser;
use Hyperf\HttpServer\Request;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Swagger\Annotation\Delete;
use Hyperf\Swagger\Annotation\Get;
use Hyperf\Swagger\Annotation\HyperfServer;
use Hyperf\Swagger\Annotation\JsonContent;
use Hyperf\Swagger\Annotation\Post;
use Hyperf\Swagger\Annotation\Put;
use Hyperf\Swagger\Annotation\RequestBody;
use Mine\Access\Attribute\Permission;
use Mine\Swagger\Attributes\PageResponse;
use App\Http\Admin\Controller\AbstractController as AdminAbstractController;
use Hyperf\Context\Context;
use Plugin\Attractions\AttractionManagement\Middleware\CheckJwtTokenMiddleware;

/**
 * 快递地址信息控制器
 * Class CommerceAddressController
 */
#[HyperfServer(name: 'http')]
#[Middleware(CheckJwtTokenMiddleware::class, priority: 100)]
final class CommerceAddressApiController extends AdminAbstractController
{

    /**
     * 业务处理服务
     * CommerceAddressService
     */
    public function __construct(
        protected readonly CommerceAddressService $service
    ) {}



    #[Get(
        path: '/commerce_address/commerceAddress/api/list',
        operationId: 'Commerce_addressCommerceaddressList',
        summary: '快递地址信息控制器列表',
        tags: ['快递地址信息控制器'],
    )]
    #[PageResponse(instance: CommerceAddressSchema::class)]
    public function page(Request $request): Result
    {
        return $this->success(data: $this->service->page(array_merge([
            'user_id' => Context::get('jwtData')['id'],
        ], $request->all()), (int) $request->query('page'), (int) $request->query('page_size')));
    }


    #[Post(
        path: '/commerce_address/commerceAddress/api',
        operationId: 'Commerce_addressCommerceaddressCreate',
        summary: '创建快递地址信息控制器',
        tags: ['快递地址信息控制器'],
    )]
    #[RequestBody(
        content: new JsonContent(ref: CommerceAddressRequest::class, title: '创建快递地址信息控制器')
    )]
    #[PageResponse(instance: new Result())]
    public function create(CommerceAddressRequest $request): Result
    {

        $this->service->apiCreate(array_merge(
            ['user_id' => intval(Context::get('jwtData')['id'])],
            $request->post()
        ));
        return $this->success();
    }

    #[Put(
        path: '/commerce_address/commerceAddress/api/{id}',
        operationId: 'Commerce_addressCommerceaddressEdit',
        summary: '编辑快递地址信息控制器',
        tags: ['快递地址信息控制器']
    )]
    #[RequestBody(
        content: new JsonContent(ref: CommerceAddressRequest::class, title: '编辑快递地址信息控制器')
    )]
    #[PageResponse(instance: new Result())]
    public function save(int $id, CommerceAddressRequest $request): Result
    {
        $this->service->apiUpdate($id, array_merge(
            ['user_id' => intval(Context::get('jwtData')['id'])],
            $request->post()
        ));
        return $this->success();
    }

    #[Delete(
        path: '/commerce_address/commerceAddress/api',
        operationId: 'Commerce_addressCommerceaddressDelete',
        summary: '删除快递地址信息控制器',
        tags: ['快递地址信息控制器']
    )]
    #[PageResponse(instance: new Result())]
    public function delete(): Result
    {
        $this->service->deleteById($this->getRequest()->all());
        return $this->success();
    }
}
